Comparison of programming languages (syntax)

Programming language comparisons
General comparison
Basic syntax
Basic instructions
Arrays
Associative arrays
String operations
String functions
List comprehension
Object-oriented programming
Object-oriented constructors
Database access
Database RDBMS

Evaluation strategy
List of "hello world" programs

ALGOL 58's influence on ALGOL 60
ALGOL 60: Comparisons with other languages
Comparison of ALGOL 68 and C++
ALGOL 68: Comparisons with other languages
Compatibility of C and C++
Comparison of Pascal and Borland Delphi
Comparison of Object Pascal and C
Comparison of Pascal and C
Comparison of Java and C++
Comparison of C# and Java
Comparison of C# and Visual Basic .NET

ToDo: add perl6, e.g. block comment

Contents

Expressions

Programming language expressions can be broadly classified in three classes:

prefix notation

infix notation

suffix, postfix, or Reverse Polish notation

Statements

Programming language statements typically have conventions for:

A statement separator is used to demarcate boundaries between two separate statements. A statement terminator is used to demarcate the end of an individual statement. Line continuation is a convention in languages where the newline character could potentially be misinterpreted as a statement terminator. In such languages, it allows a single statement to span more than just one line.

Language Statement separator/terminator Secondary separator[1]
ABAP period separated
Ada semicolon separated
ALGOL semicolon separated
ALGOL 68 semicolon and comma separated[2]
AppleScript newline terminated
AutoHotkey newline terminated
BASIC newline terminated colon
Boo newline terminated
C semicolon terminates statements comma separates expressions
C++ semicolon terminates statements comma separates expressions
C# semicolon terminated
COBOL period separated
Cobra newline terminated
D semicolon terminated
Eiffel newline terminated semicolon
Erlang colon separated, period terminated
Fortran newline terminated semicolon
Forth  ? whitespace
GFA BASIC newline terminated
Go semicolon separated (inserted by compiler)
Haskell (in do-notation) semicolon separated
Haskell (in do-notation, when braces are omitted) newline separated
Java semicolon terminated
JavaScript semicolon separated (but sometimes implicitly inserted on newlines)
Lua whitespace separated (semicolon optional)
MATLAB newline terminated semicolon or comma[3]
Object Pascal (Delphi) semicolon separated
Objective-C semicolon terminated
OCaml semicolon separated
Pascal semicolon separated
Perl semicolon separated
PHP semicolon terminated
Prolog period terminated
Python newline terminated semicolon
Ruby newline terminated semicolon
Scala newline terminated (semicolon optional) semicolon
Simula semicolon separated
S-Lang semicolon separated
Smalltalk period separated
Standard ML semicolon separated
Visual Basic newline terminated
Visual Basic .NET newline terminated
Windows PowerShell newline terminated semicolon separated
Language Statement separator/terminator Secondary separator[1]

Line continuation

Whitespace - Languages that do not need continuations

Ampersand as last character of line

Backslash as last character of line

Backtick as last character of line

Hyphen as last character of line

Left parenthesis as last character of line

Underscore as last character of line

Ellipsis (as three periods–not one special character)

Some form of inline comment serves as line continuation

Character position

[End and Begin] using normal quotes

Libraries

To import a library is a way to read external, possibly compiled, routines, programs or packages. Imports can be classified by level (module, package, class, procedure,...) and by syntax (directive name, attributes,...)

File import

Package import

Class import

Procedure/function import

The above statements can also be classified by whether they are a syntactic convenience (allowing things to be referred to by a shorter name, but they can still be referred to by some fully qualified name without import), or whether they are actually required to access the code (without which it is impossible to access the code, even with fully qualified names).

Syntactic convenience

Required to access code

Blocks

A block is a notation for a group of two or more statements, expressions or other units of code that are related in such a way as to comprise a whole.

Braces (aka Curly brackets) { }:

Parentheses

begin ... end:

do ... done:

do ... end

X ... end (e.g. if ... end):

(begin ...):

(progn ...):

(do ...):

Indentation

Others

Comments

Comments can be classified by:

Inline comments

Inline comments are generally those that use a newline character to indicate the end of a comment, and an arbitrary delimiter or sequence of tokens to indicate the beginning of a comment.

Examples:

Symbol Languages
C Fortran 77 and earlier; the 'C' must be in column 1 of a line to indicate a comment.
REM BASIC, COMMAND.COM, cmd.exe
NB. J; from the (historically) common abbreviation Nota bene, the Latin for "note well".
APL; the mnemonic is the glyph (jot overstruck with shoe-down) resembles a desk lamp, and hence "illuminates" the foregoing.
# bash, Cobra, Perl, Python, Ruby, Windows PowerShell, PHP, Maple, httpd.conf
% TeX, Prolog, MATLAB,[7] Erlang, S-Lang, Visual Prolog
// ActionScript, C (C99), C++, C#, D, Go, Java, JavaScript, Object Pascal (Delphi), Objective-C, PHP, Scala
' Visual Basic, VBScript, RealBasic
! Fortran, Basic Plus, Inform
; AutoHotkey, AutoIt, Lisp, Common Lisp, Clojure, Rebol, Scheme, many assemblers, Windows .ini and .reg files
-- Euphoria, Haskell, SQL, Ada, AppleScript, Eiffel, Lua, VHDL
* COBOL, PAW, many assemblers
|| Curl
" Vimscript
\ Forth
:: Batch file[8]

Block comments

Block comments are generally those that use a delimiter to indicate the beginning of a comment, and another delimiter to indicate the end of a comment. In this context, whitespace and newline characters are not counted as delimiters.

Examples:

Symbol Languages
¢ ~ ¢, # ~ #, co ~ co, comment ~ comment ALGOL 68
/* */ ActionScript, AutoHotkey, C, C++, C#, D, Go, Java, JavaScript, Objective-C, PHP, PL/I, Scala (can be nested), SQL, Visual Prolog, CSS
#cs #ce AutoIt
/+ +/ D (can be nested)
/# #/ Cobra (can be nested)
<# #> Powershell
=begin =cut Perl
=begin =end Ruby
#<tag> #</tag> S-Lang
{- -} Haskell
(* *) Object Pascal (Delphi), ML, Mathematica, Pascal, Applescript, OCaml (can be nested), Standard ML (can be nested), Maple
{ } Object Pascal (Delphi), Pascal
|# #| Curl
 %{ %} MATLAB[7]
#| |# Lisp
--[[ ]] Lua
" " Smalltalk
(comment ...) Clojure

Unique variants

Fortran

Cobra

Curl

Lua

Perl

PHP

Python

Ruby

S-Lang

Haskell

Esoteric languages

Comment comparison

There is a wide variety of syntax styles for declaring comments in source code. BlockComment in italics is used here to indicate block comment style. InlineComment in italics is used here to indicate inline comment style.

Language In-line comment Block comment
Ada, Eiffel, Euphoria, Occam, SPARK, ANSI SQL, ToolBook OpenScript, and VHDL -- InlineComment
ALGOL 60 comment BlockComment;
ALGOL 68 ¢ BlockComment ¢

comment BlockComment comment
co BlockComment co
# BlockComment #
£ BlockComment £

AppleScript -- InlineComment (* BlockComment *)
Assembly language (varies) ; InlineComment   one example (most assembly languages use line comments only)
AutoHotkey ; InlineComment /* BlockComment */
AWK, Bash, Bourne shell, C shell, Maple, Python, R, Tcl, and Windows PowerShell # InlineComment <# BlockComment #>
BASIC (various dialects): 'InlineComment (not all dialects)

REM InlineComment

C (K&R, ANSI/C89/C90), CHILL, CSS, PL/I, and REXX /* BlockComment */
C (C99), C++, Go, and JavaScript // InlineComment /* BlockComment */
C# // InlineComment
/// InlineComment (XML documentation comment)
/* BlockComment */
/** BlockComment */ (XML documentation comment)
Cobol InlineComment (when * is in column 7)
Curl || InlineComment |# BlockComment #|

|foo# BlockComment #|

Cobra # InlineComment /# BlockComment #/ (nestable)
D // InlineComment
/// Documentation InlineComment, (ddoc comments)
/* BlockComment */
/** Documentation BlockComment */, (ddoc comments)

/+ BlockComment +/ (nestable)
/++ Documentation BlockComment +/ (nestable, ddoc comments)

DCL $! InlineComment
ECMAScript (JavaScript, ActionScript, etc.) // InlineComment /* BlockComment */
Forth \ InlineComment ( BlockComment ) (single line only)

( before -- after ) stack comment convention

FORTRAN 66/77 C InlineComment (the letter 'C' in the first column makes the entire line a comment).
Fortran 90 ! InlineComment (all characters on the line, from the exclamation mark onwards, are comments)
HTML (see SGML below)
Java: // InlineComment /* BlockComment */

/** BlockComment */ (Javadoc documentation comment)

Lisp and Scheme ; InlineComment #| BlockComment |#
Lua -- InlineComment --[==[ BlockComment]==] (variable number of = signs)
Maple # InlineComment (* BlockComment *)
Mathematica % (* BlockComment *)
Matlab % InlineComment %{
BlockComment (nestable)
%}

Note: Both percent–bracket symbols must be the only non-whitespace characters on their respective lines.
Object Pascal (Delphi) // InlineComment (* BlockComment *)
{ BlockComment }
Ocaml (* BlockComment (* nestable *) *)
Pascal, Modula-2, Modula-3, Oberon, and ML: (* BlockComment *) (OCaml comments are nestable)
Perl and Ruby # InlineComment =begin
BlockComment
=cut
(POD documentation comment)

__END__
Comments after end of code

PHP # InlineComment
// InlineComment
/* BlockComment */
PILOT R:InlineComment
PL/SQL and TSQL -- InlineComment /* BlockComment */
REALbasic ' InlineComment
// InlineComment
rem InlineComment
SAS * BlockComment;
/* BlockComment */
Seed7 # InlineComment (* BlockComment *)
Simula comment BlockComment;
! BlockComment;
SGML, including HTML :A comment declaration starts with <!, followed by zero or more comments, followed by >. A comment starts and ends with --, and does not contain any occurrence of --. Valid examples are:
  • <!-- BlockComment -- -- BlockComment -->,
  • <!------ BlockComment -->, or
  • <!>.
Smalltalk "BlockComment"
Smarty {* BlockComment *}
Standard ML (* BlockComment *)
TeX, LaTeX, PostScript, Erlang, and S-Lang % InlineComment
Texinfo @c InlineComment

@comment InlineComment

TUTOR * InlineComment
command $$ InlineComment
Visual Basic ' InlineComment
Rem InlineComment
Visual Basic .NET ' InlineComment

''' InlineComment (XML documentation comment)
Rem InlineComment

Visual Prolog % InlineComment /* BlockComment */
XML, including XHTML <!--BlockComment--> (comment must not contain -- and must not start or end with single -)

See also

References

  1. ^ a b For multiple statements on one line
  2. ^ Three different kinds of clauses, each separates phrases and the units differently:
      1. serial-clause using go-on-token (viz. semicolon): begin a; b; c end - units are executed in order.
      2. collateral-clause using and-also-token (viz. “,”): begin a, b, c end - order of execution is to be optimised by the compiler.
      3. parallel-clause using and-also-token (viz. “,”): par begin a, b, c end - units must be run in parallel threads.
  3. ^ semicolon - result of receding statement hidden, comma - result displayed
  4. ^ http://ss64.com/nt/syntax-brackets.html
  5. ^ Mathworks.com
  6. ^ For an M-file (MATLAB source) to be accessible by name, its parent directory must be in the search path (or current directory).
  7. ^ a b Mathworks.com
  8. ^ SS64.com